package com.mutouren.modules.org.client.proxy;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.mutouren.common.entity.Result;
import com.mutouren.common.entity.ResultCode;
import com.mutouren.common.entity.ResultInfo;
import com.mutouren.common.log.LogAlias;
import com.mutouren.common.log.LogManager;
import com.mutouren.common.log.Logger;
import com.mutouren.common.session.UserInfo;
import com.mutouren.common.utils.HttpClientUtils;
import com.mutouren.common.utils.HttpResult;
import com.mutouren.modules.org.client.config.ClientConfig;
import com.mutouren.modules.org.model.Function;

public class OrgApiProxy {
	
	//private static ClientConfig clientConfig = ClientConfig.getInstance();
	private static String apiContextUrl = ClientConfig.getApiContextUrl();
	private static Logger errorLogger = LogManager.getLogger(LogAlias.SystemError.name());
	
	private static String getUrl(String methodName) {
		return String.format("%s/api/org/%s.action", apiContextUrl, methodName);
	}
	
	public static ResultInfo<UserInfo> login(String loginName, String password) {		
		String url = getUrl("login");
		ResultInfo<UserInfo> result;
		
		HttpClientUtils httpClient = new HttpClientUtils(url, "GET");
		httpClient.addParameter("loginName", loginName);		
		httpClient.addParameter("password", password);
		
		HttpResult httpResult;
		try {
			httpResult = httpClient.execute();

			if(httpResult.isSuccess()) {
				String body = httpResult.getContent("utf-8");
				result = JSON.parseObject(body, new TypeReference<ResultInfo<UserInfo>>(){});  
			} else {
				result = new ResultInfo<UserInfo>(ResultCode.EXCEPTION.value, "OrgApiProxy.login: request fail");
			}
		} catch(Throwable t) {
			result = new ResultInfo<UserInfo>(ResultCode.EXCEPTION.value, t.getMessage());
			errorLogger.error("OrgApiProxy.login error", t);
		} finally {
			// log
		}
		
		return result;
	}
	
	public static ResultInfo<UserInfo> mappingUser(String identity, String identityType) {
		String url = getUrl("mappingUser");
		ResultInfo<UserInfo> result;
		
		HttpClientUtils httpClient = new HttpClientUtils(url, "GET");
		httpClient.addParameter("identity", identity);		
		httpClient.addParameter("identityType", identityType);
		
		HttpResult httpResult;
		try {
			httpResult = httpClient.execute();

			if(httpResult.isSuccess()) {
				String body = httpResult.getContent("utf-8");
				result = JSON.parseObject(body, new TypeReference<ResultInfo<UserInfo>>(){});  
			} else {
				result = new ResultInfo<UserInfo>(ResultCode.EXCEPTION.value, "OrgApiProxy.mappingUser: request fail");
			}
		} catch(Throwable t) {
			result = new ResultInfo<UserInfo>(ResultCode.EXCEPTION.value, t.getMessage());
			errorLogger.error("OrgApiProxy.mappingUser error", t);
		} finally {
			// log
		}
		
		return result;
	}
	
	public static Result modifyPassword(int userId, String oldPassword, String newPassword) {
		String url = getUrl("modifyPassword");
		Result result;
		
		HttpClientUtils httpClient = new HttpClientUtils(url, "GET");
		httpClient.addParameter("userId", Integer.toString(userId));		
		httpClient.addParameter("oldPassword", oldPassword);
		httpClient.addParameter("newPassword", newPassword);
		
		HttpResult httpResult;
		try {
			httpResult = httpClient.execute();

			if(httpResult.isSuccess()) {
				String body = httpResult.getContent("utf-8");
				result = JSON.parseObject(body, Result.class);  
			} else {
				result = new Result(ResultCode.EXCEPTION.value, "OrgApiProxy.modifyPassword: request fail");
			}
		} catch(Throwable t) {
			result = new ResultInfo<UserInfo>(ResultCode.EXCEPTION.value, t.getMessage());
			errorLogger.error("OrgApiProxy.modifyPassword error", t);
		} finally {
			// log
		}
		
		return result;
	}
	
	public static ResultInfo<List<Function>> getFunctionsOfApp(String appAlias) {
		String url = getUrl("getFunctionsOfApp");
		ResultInfo<List<Function>> result;
		
		HttpClientUtils httpClient = new HttpClientUtils(url, "GET");
		httpClient.addParameter("appAlias", appAlias);		
		
		HttpResult httpResult;
		try {
			httpResult = httpClient.execute();

			if(httpResult.isSuccess()) {
				String body = httpResult.getContent("utf-8");
				result = JSON.parseObject(body, new TypeReference<ResultInfo<List<Function>>>(){});  
			} else {
				result = new ResultInfo<List<Function>>(ResultCode.EXCEPTION.value, "OrgApiProxy.getFunctionsOfApp: request fail");
			}
		} catch(Throwable t) {
			result = new ResultInfo<List<Function>>(ResultCode.EXCEPTION.value, t.getMessage());
			errorLogger.error("OrgApiProxy.getFunctionsOfApp error", t);
		} finally {
			// log
		}
		
		return result;
	}
	
	public static ResultInfo<int[]> getFunctionIdsOfUser(int userId, String appAlias) {
		String url = getUrl("getFunctionIdsOfUser");
		ResultInfo<int[]> result;
		
		HttpClientUtils httpClient = new HttpClientUtils(url, "GET");
		httpClient.addParameter("userId", Integer.toString(userId));	
		httpClient.addParameter("appAlias", appAlias);	
		
		HttpResult httpResult;
		try {
			httpResult = httpClient.execute();

			if(httpResult.isSuccess()) {
				String body = httpResult.getContent("utf-8");
				result = JSON.parseObject(body, new TypeReference<ResultInfo<int[]>>(){});  
			} else {
				result = new ResultInfo<int[]>(ResultCode.EXCEPTION.value, "OrgApiProxy.getFunctionIdsOfUser: request fail");
			}
		} catch(Throwable t) {
			result = new ResultInfo<int[]>(ResultCode.EXCEPTION.value, t.getMessage());
			errorLogger.error("OrgApiProxy.getFunctionIdsOfUser error", t);
		} finally {
			// log
		}
		
		return result;
	}
}
